<?php

namespace App\Exports;

use App\Models\Student\Ask;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;

class AskExport extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{
    private $conditions;

    public function __construct(array $conditions) {
        $this->conditions = $conditions;
    }

    public function array():array  {
        $data = [
            [
                'ID',
                '姓名',
                '电话',
                '微信',
                '咨询时间',
            ]
        ];


        $cols = ['id','name','phone','wx','created_at'];
        $records = (new Ask())->getRecords($this->conditions)->get($cols);

        foreach ($records as &$record) {
            //$record->account = $record->getGuardUser->name;
            //unset($record['updated_at']);
            //unset($record->getGuardUser);
            $data[] = $record->toArray();
        }
        return $data;
    }

    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
